package com.wjc.myktapplication.cusview

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.core.view.children
import com.wjc.basemodule.utils.LogTools
import kotlin.math.max

/**
 * ClassName: com.wjc.myktapplication.cusview
 * Description:
 * JcChen on 2021.09.03.08:21
 */
class TagLayout(context: Context, attrs: AttributeSet) : ViewGroup(context, attrs) {
    val TAG = "TagLayout"
    private val childrenBounds = mutableListOf<Rect>()

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var widthUsed = 0
        var heightUsed = 0
        var lineMaxHeight = 0
        var lineWidthUsed = 0
        val specWidthMode = MeasureSpec.getMode(widthMeasureSpec)
        val specWidthSize = MeasureSpec.getSize(widthMeasureSpec)

        for ((index, child) in children.withIndex()) {

            // 计算子View 的 mode、size
            measureChildWithMargins(
                child,
                widthMeasureSpec,
                0,
                heightMeasureSpec,
                heightUsed
            )
            // 超过一行，重置
//            LogTools.d(
//                TAG,
//                "specWidthSize=$specWidthSize，lineWidthUsed=$lineWidthUsed,child.measuredWidth=${child.measuredWidth}"
//            )
            if (specWidthMode != MeasureSpec.UNSPECIFIED && lineWidthUsed + child.measuredWidth > specWidthSize) {
                lineWidthUsed = 0
                heightUsed += lineMaxHeight
                lineMaxHeight = 0
                measureChildWithMargins(
                    child,
                    widthMeasureSpec,
                    lineWidthUsed,
                    heightMeasureSpec,
                    heightUsed
                )
            }

            if (index >= childrenBounds.size) {
                childrenBounds.add(Rect())
            }

            val childBound = childrenBounds[index]
//            LogTools.d(TAG, ": widthUsed=$widthUsed,heightUsed=$heightUsed");
            childBound.set(
                lineWidthUsed,
                heightUsed,
                lineWidthUsed + child.measuredWidth,
                heightUsed + child.measuredHeight
            )
            lineWidthUsed += child.measuredWidth
            widthUsed = max(lineWidthUsed, widthUsed)
            lineMaxHeight = max(lineMaxHeight, child.measuredHeight)
        }
        val selfWidth = widthUsed
        val selfHeight = lineMaxHeight + heightUsed
//        LogTools.d(TAG, "selfWidth=$selfWidth,selfHeight=$selfHeight ");
        setMeasuredDimension(selfWidth, selfHeight)

    }


    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        for ((index, child) in children.withIndex()) {
            child.layout(
                childrenBounds[index].left,
                childrenBounds[index].top,
                childrenBounds[index].right,
                childrenBounds[index].bottom
            )
        }
    }

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        return MarginLayoutParams(context, attrs)
    }

}